package client.neo4j;

import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;

public class AlgoOps {


    public static StatementResult findShortestPath(Session session, Long startId, Long endId){
        String sql = " MATCH (p1), (p2) " +
                " WHERE id(p1) = " + startId + " AND id(p2) =  " + endId +
                " CALL algo.shortestPath.stream(p1, p2)  YIELD nodeId, cost " +
                " RETURN algo.asNode(nodeId) as node, cost ";
        return session.run(sql);
    }

    public static StatementResult getNodeSimilarities(Session session, Long startId, Long endId) {
        String sql = " MATCH (p1)--(relatives1) , (p2)--(relatives2) " +
                " WHERE id(p1) = " + startId + " AND id(p2) =  " + endId +
                " WITH p1, collect(id(relatives1)) AS p1relatives , p2, collect(id(relatives2)) AS p2relatives  \n" +
                " RETURN p1,p2,  algo.similarity.jaccard(p1relatives, p2relatives) AS similarity ";
        return session.run(sql);
    }

    public static StatementResult getLouvainCommunities(Session session) {
        String sql = " CALL algo.louvain.stream('Enterprise', 'INVEST', {includeIntermediateCommunities: true} ) " +
                " YIELD nodeId, community " +
                " RETURN algo.asNode(nodeId).enterpriseName AS name, community " +
                " ORDER BY community ";
        return session.run(sql);
    }

    public static StatementResult getLPCommunities(Session session){
        String sql = " CALL algo.labelPropagation.stream('Enterprise', 'INVEST', {direction: 'BOTH'} )  " +
                "YIELD nodeId, label " +
                " RETURN algo.asNode(nodeId).enterpriseName AS name, label " +
                " ORDER BY label ";
        return session.run(sql);
    }

}
